﻿using sdd.subsystems.display;
using System;

// SDD# - Software Defined Devices in C# for Linux and Windows
// This screen displays a simple clock
// (c) 2014-2015 Chris Burri. MIT License applies. 
// See http://niston.wordpress.com

namespace sdd.subsystems.ui.screens
{
    class ScrClock : IScreen
    {
        private System.Timers.Timer _ClockUpdateTimer;
        private DateTime _LastUpdate;

        public ScrClock()
        {
            _ClockUpdateTimer = new System.Timers.Timer();
            _ClockUpdateTimer.Elapsed += ClockUpdateTimer_OnElapsed;
            _ClockUpdateTimer.Interval = 300;
        }

        ~ScrClock()
        {
            _ClockUpdateTimer.Elapsed -= ClockUpdateTimer_OnElapsed;
            _ClockUpdateTimer.Stop();
        }


        public display.IDisplay Display { set; get; }
        public apps.IApplication Application { set; get; }
        public ScreenManager Manager { set; get; }

        public string ScreenName
        {
            get { return "Clock"; }
        }
       
        public void Initialize()
        {
            // register menu entry
            Manager.Menus.Register(new LinkMenuItem("app.clock.display", "Clock", this), Manager.Menus.Menu("Application"));
            
            // setup labels
            Display.InitializeLabel(ScreenManager.LABEL_CLOCK_TIME, 42, 1, 192, 62, JustificationVertical.Top, JustificationHorizontal.Right, ScreenManager.FONT_PROP_XLARGE, 0, 2);
            Display.InitializeLabel(ScreenManager.LABEL_CLOCK_AMPM, 14, 7, 40, 24, JustificationVertical.Top, JustificationHorizontal.Left, ScreenManager.FONT_PROP_LARGE, 0, 1);
            Display.InitializeLabel(ScreenManager.LABEL_CLOCK_DATE, 1, 55, 190, 64, JustificationVertical.Top, JustificationHorizontal.Right, ScreenManager.FONT_PROP_SMALL, 0, 1);
        }

        public void Show()
        {
            Refresh();            
            _ClockUpdateTimer.Start();
        }

        public void Hide()
        {
            _ClockUpdateTimer.Stop();
            Display.ClearScreen();
        }

        public void ProcessKeyPadEvent(display.KeypadEventArgs keyPadEventArgs)
        {
            if (keyPadEventArgs.Key == display.KeypadKeys.App)
            {
                Manager.Buzz();
                Manager.Show(Manager.PreviousScreen);
            }
        }
        
        private void ClockUpdateTimer_OnElapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            UpdateClock();
        }

        private void Refresh()
        {
            UpdateClock(true);
        }

        private void UpdateClock(bool forceUpdate = false)
        {
            if ((DateTime.Now.Hour != _LastUpdate.Hour | DateTime.Now.Minute != _LastUpdate.Minute) | forceUpdate)
            {
                string clockText = string.Format("{0:00}", string.Format("{0:HH}", DateTime.Now)) + ":" + string.Format("{0:00}", string.Format("{0:mm}", DateTime.Now));
                string dateText = DateTime.Now.Date.ToLongDateString();
                Display.UpdateLabel(ScreenManager.LABEL_CLOCK_TIME, clockText);
                Display.UpdateLabel(ScreenManager.LABEL_CLOCK_AMPM, string.Format("{0:tt}", DateTime.Now));
                Display.UpdateLabel(ScreenManager.LABEL_CLOCK_DATE, dateText);
                _LastUpdate = DateTime.Now;
            }
        }



    }
}
